package com.supermarket.seefood51.home

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.supermarket.seefood51.R
import com.supermarket.seefood51.entity.SeaFoodEntity

class ProductAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    val TYPE_TITLE = 1
    val TYPE_ITME = 2
    val TYPE_MENU = 3
    var list: ArrayList<SeaFoodEntity> = ArrayList()

    override fun getItemViewType(position: Int): Int {
        return list[position].type
    }


    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_TITLE ->
                TitleViewHolder(getRootId(parent!!, R.layout.item_product_title))
            TYPE_ITME ->
                ItemViewHolder(getRootId(parent!!, R.layout.item_product_item))
            TYPE_MENU ->
                MenuViewHolder(getRootId(parent!!, R.layout.item_product_menu))
            else -> {
                MenuViewHolder(getRootId(parent!!, R.layout.item_product_menu))
            }
        }
    }

    private fun getRootId(parent: ViewGroup, rootId: Int): View {
        return LayoutInflater.from(parent.context).inflate(rootId, parent, false)
    }


    override fun getItemCount(): Int {

        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        when (list[position].type) {
            TYPE_ITME ->
                (holder as ItemViewHolder).setData(list[position])
            TYPE_MENU ->
                (holder as MenuViewHolder).setData(list[position])
            TYPE_TITLE ->
                (holder as TitleViewHolder).setData(list[position])
        }

    }

    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        var img: ImageView = view.findViewById(R.id.img)
        var price: TextView = view.findViewById(R.id.price)
        var title: TextView = view.findViewById(R.id.title)

        fun setData(entity: SeaFoodEntity) {

        }

    }

    inner class TitleViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        var title = view as TextView

        fun setData(entity: SeaFoodEntity) {

        }

    }

    inner class MenuViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        var title = view as TextView

        fun setData(entity: SeaFoodEntity) {
            title.setOnClickListener {
//                currentMenuChecked =
            }
        }

    }
}